#include <bits/stdc++.h>
using namespace std;
const int M = 2000, N = 1100;
int tot = 0;
int R[N][M], G[N][M], B[N][M];
int sr[N][M], sg[N][M], sb[N][M];
typedef long long ll;
int m, n, p, q;
inline char getc(int x) {
  if (x <= 9) return x + '0';
  return x - 10 + 'A';
}
inline int h2i(char c) {
  if (isdigit(c)) {
    return c - '0';
  } else {
    return c - 'a' + 10;
  }
};
tuple<int, int, int> dec(string& s) {
  int len = s.size();
  int r, g, b;
  r = g = b = 0;
  if (len == 7) {
    r = h2i(s[1]) * 16 + h2i(s[2]);
    g = h2i(s[3]) * 16 + h2i(s[4]);
    b = h2i(s[5]) * 16 + h2i(s[6]);
  } else if (len == 4) {
    r = h2i(s[1]) * 16 + h2i(s[1]);
    g = h2i(s[2]) * 16 + h2i(s[2]);
    b = h2i(s[3]) * 16 + h2i(s[3]);
  } else if (len == 2) {
    r = h2i(s[1]) * 16 + h2i(s[1]);
    g = h2i(s[1]) * 16 + h2i(s[1]);
    b = h2i(s[1]) * 16 + h2i(s[1]);
  } else
    assert(false);
  return {r, g, b};
}
// printf("\\x%02X", x); 有坑
inline void push(char x) { printf("\\x%c%c", getc(x / 16), getc(x % 16)); }
void push(string x) {
  for (char c : x) push(c);
}
void input() {
  cin >> m >> n >> p >> q;
  cin.get();
  string buf;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      getline(cin, buf);
      auto [r, g, b] = dec(buf);
      R[i][j] = r, G[i][j] = g, B[i][j] = b;
    }
  }
}
tuple<int, int, int> avg(int x, int y) {
  ll r, g, b;
  r = g = b = 0;
  for (int i = 0; i < q; ++i) {
    for (int j = 0; j < p; ++j) {
      r += R[x * q + i][y * p + j];
      g += G[x * q + i][y * p + j];
      b += B[x * q + i][y * p + j];
    }
  }
  int sz = p * q;
  r /= sz, g /= sz, b /= sz;
  return {r, g, b};
}
void process() {
  for (int i = 0; i < n / q; ++i) {
    for (int j = 0; j < m / p; ++j) {
      auto [r, g, b] = avg(i, j);
      sr[i][j] = r, sg[i][j] = g, sb[i][j] = b;
    }
  }
}
string get(int r, int g, int b) {
  string base = "\033[48;2;";
  base += to_string(r);
  base += ';';
  base += to_string(g);
  base += ';';
  base += to_string(b);
  base += 'm';
  return base;
}

void output() {
  int br, bg, bb;
  br = bg = bb = 0;
  for (int i = 0; i < n / q; ++i) {
    for (int j = 0; j < m / p; ++j) {
      int r = sr[i][j], b = sb[i][j], g = sg[i][j];
      if (r == br && g == bg && b == bb)
        ;
      else if (!r && !g && !b)
        push("\033[0m");
      else {
        push(get(r, g, b));
      }
      br = r, bb = b, bg = g;
      push(' ');
    }
    if (br || bg || bb) {
      push("\033[0m");
      br = bg = bb = 0;
    }
    push('\n');
  }
}

int main() {
  input();
  process();
  output();
}
